// 网关配置的前缀
let account_Server = "/accountSer"
let publicKey = "";

// let userId = "1";
let userId = "";

// 输入的原密码 , 以及是否无误
var password_value, isCorrect_1 = false;
// 两次新密码
var first_new_pwd, isCorrect_2 = false;
var second_new_pwd, isCorrect_3 = false;

let Authorization = "";
let refreshToken = "";
// 加载页面时读取后台的 ip
window.onload = function () {
    $.ajaxSettings.async = false;
    $.getJSON("/js/properties.json", function (data) {
        account_Server = data.accountServer;
    });
    $.ajax({
        url: "/js/publicKey.txt",
        success: function(data) {
            publicKey = data;
        }
    });

    // 获取令牌
    Authorization = getCookie("Authorization")
    refreshToken = getCookie("refreshToken")
    userId = $("#userId").html();
}

// 设置提交按钮状态
// isSubmit : 是否 可以 提交
function change_but_status(isSubmit) {
    if (isSubmit) {// 可以提交
        $("#submit").attr('disabled', false);
        $("#submit").css('background-color', '#1dae0e');
    } else {// 不可提交
        $("#submit").attr('disabled', true);
        $("#submit").css('background-color', '#808080c2');
    }
}

// 判断是否修改按钮状态的方法
function is_change() {
    change_but_status(isCorrect_1 && isCorrect_2 && isCorrect_3);
}

// 判断密码输入
$('#password').change(function () {
    password_value = $(this).val();
    const encrypt = new JSEncrypt()
    encrypt.setPublicKey(publicKey)  // 此处设置公钥
    let encodedPassword = encrypt.encrypt(password_value)  // 这里对密码进行加密
    $.ajax({
        type: "GET",
        async: false,
        url: account_Server + "/account/changePassword",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            userId: userId,
            password: encodedPassword,
        },
        success: function (data) {
            if (data.resp_code === 0){
                cocoMessage.info(2000, data.resp_msg);
                $('#error-1').html("");
                isCorrect_1 = true;
            }else {
                cocoMessage.error(2000, data.resp_msg);
                $('#error-1').html("原密码输入错误");
                isCorrect_1 = false;
            }
            is_change();
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText);
        }
    });
});

// 新密码 第一次输入异常
$('#first_new_pwd').change(function () {
    first_new_pwd = $(this).val();

    if (first_new_pwd === password_value) {
        $('#error-2').html("新密码与原密码相同");
        isCorrect_2 = false;
    } else if (first_new_pwd === null || first_new_pwd === "") {
        $('#error-2').html("新密码不能为空");
        isCorrect_2 = false;
    } else {
        $('#error-2').html("");
        isCorrect_2 = true;
    }
    second_pwd_correct();
    if (isCorrect_3) {// 如果输入正确
        $('#error-3').html("");
    } else {// 如果输入不正确
        $('#error-3').html("两次新密码不一致");
    }
    is_change();
});

// 新密码 第二次输入异常
function second_pwd_correct() {
    second_new_pwd = $('#second_new_pwd').val();
    if (second_new_pwd !== first_new_pwd) {
        isCorrect_3 = false;
    } else {
        isCorrect_3 = true;
    }
}

$('#second_new_pwd').change(function () {
    second_pwd_correct();
    if (isCorrect_3) {// 如果输入正确
        $('#error-3').html("");
    } else {// 如果输入不正确
        $('#error-3').html("两次新密码不一致");
    }
    is_change();
});

function submitPassword() {
    const encrypt = new JSEncrypt()
    encrypt.setPublicKey(publicKey)  // 此处设置公钥
    let encodedPassword = encrypt.encrypt(first_new_pwd)  // 这里对密码进行加密

    $.ajax({
        type: "POST",
        async: false,
        url: account_Server + "/account/changePassword",
        headers: {
            "Authorization": Authorization,
            "refreshToken": refreshToken,
        },
        data: {
            userId: userId,
            newPassword: encodedPassword,
        },
        success: function (data) {
            if (data.resp_code === 0){
                cocoMessage.info(2000, data.resp_msg);
                isCorrect_2 = false;
                isCorrect_3 = false;
            }else {
                cocoMessage.error(2000, data.resp_msg);
            }
            is_change();
        },
        error: function (err) {
            // 异常
            cocoMessage.error(2000, err.statusText);
        }
    });
}
